import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'


Vue.config.productionTip = false

// 初始化跨平台的工具类。
import SDK from 'sxran-tool-sdk/Index'
import XeBase from 'sxran-tool-core/lib/XeBase';
import UniAppWebSocket from "sxran-tool-uniapp/lib/UniAppWebSocket";
import UniAppBroadcastChannel from "sxran-tool-uniapp/lib/UniAppBroadcastChannel";
import UniAppStorage from "sxran-tool-uniapp/lib/UniAppStorage";

const sdk = new SDK();
const xeBase = new XeBase();
const uniAppWebSocket = new UniAppWebSocket();
const uniAppBroadcastChannel = new UniAppBroadcastChannel();
const uniAppStorage = new UniAppStorage();

sdk.setApi(SDK.BASE_NAME,xeBase);
sdk.setApi(SDK.WEBSOCKET_NAME,uniAppWebSocket);
sdk.setApi(SDK.BROADCAST_CHANNEL_NAME,uniAppBroadcastChannel);
sdk.setApi(SDK.STORAGE_NAME,uniAppStorage);
Vue.prototype.$sxran = sdk;

// 获得ws的内容。
import ws from 'lib/websocket'
ws.copySdk(sdk);
Vue.prototype.$ws = ws;
////////////////////////
// 封装一下特别的方法内容。
////////////////////////

// 初始化设置tabbar角标未读数。
// 注意：在非tabbar页面无法设置 badge
 function initUnreadCount () {
  // 会话设置
  // 系统通知设置
  let unreadCount = ws.state.notification.unreadCount({excludeType: [46]});
  if (unreadCount === 0) {
      uni.removeTabBarBadge({
          index: 1,
          complete: (e) => {
              console.log(e)
          }
      })
  } else {
      uni.setTabBarBadge({
          index: 1,
          text: unreadCount + '',
          complete: (e) => {
              console.log(e)
          }
      })
  }
}
Vue.prototype.initUnreadCount = initUnreadCount;
// 设置状态栏监听。
ws.setMonitor([26,-28],(res)=>{
    initUnreadCount(); // 系统通知消息提示。
});


function init(){
        // #ifdef H5
        window.addEventListener('resize', () => {
            setIsWidescreen()
        })
        function setIsWidescreen() {
            let oldState = ws.state.isWidescreen
            ws.state.isWidescreen = window.innerWidth > 960
            if (oldState == false && ws.state.isWidescreen == true) {
                // if (!window.location.href.includes('/uni_modules/uni-im/pages/index/index')) {
                // 	console.log('uni-im检测到窗口由小屏切为大屏。')
                // 	uni.showModal({
                // 		content: '检测到你的窗口已改成pc模式，是否切换显示模式',
                // 		cancelText: "不用了",
                // 		confirmText: "立即切换",
                // 		complete(e) {
                // 			if (e.confirm) {
                // 				uni.switchTab({
                // 					url: "/uni_modules/uni-im/pages/index/index",
                // 					success() {
                // 						window.location.reload()
                // 					}
                // 				})
                // 			}
                // 		}
                // 	});
                // }
            }
        }
        setIsWidescreen()
        // #endif
        const audioContext = uni.createInnerAudioContext()
        let _audioContext = {}
        Object.defineProperty(_audioContext,'src',{
            set(url) {
                audioContext.src = url
            },
            get() {
                return audioContext.src
            }
        })
        ws.state.audioContext = new Proxy(_audioContext,{
            get(target,propKey,receiver){
                return audioContext[propKey]
            }
        })

        // 设置系统信息。
        ws.state.systemInfo = uni.getSystemInfoSync()
}
init();

App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif